#include <algorithm> // A new header file
#include <set>
#include <vector>
#include <iostream>

using namespace std;

vector<int> twoNumberSum(vector<int> array, int targetSum) {
  set<int> coll;

  for (int n : array) {
    int num = targetSum - n;
    
    // Iterator
    // 找到：set<int>::iterator -> num 在 set 中的位置
    // 找不到：coll.end()
    if (coll.find(num) != coll.end()) {
      return {n, num};
    }
    else {
      coll.insert(n);
    }
  }

  return {};
}


int main(int argc, char const *argv[])
{
  /* code */
  // II - iterators
  vector<int> array {8, 5, 9, 1, 7, 6, 2};

  auto result = twoNumberSum(array, 15);

  cout<<"Result:";
  for (int n : result) {
    cout<<n<<" ";
  }
  cout<<endl;

  //                 ^beg
  // Traidional ways of indexing an array
  // array[0] -> 8
  // array[6] -> 2
  // 0
  // array[0]
  //
  // Modern ways of indexing an array
  // Iterators
  auto beg = array.begin(); // Iterator that points to the beginning of an array
  cout<<*beg<<endl;

  ++beg;
  cout<<*beg<<endl;

  // {8, 5, 9, 1, 7, 6, 2}
  //     ^beg
  //                    ^ end()
  vector<int>::iterator end = array.end(); // end() 返回数组中最后一个元素的下一个位置
  --end;
  cout<<*end<<endl;
  --end;
  cout<<*end<<endl;

  sort(array.begin() + 1, array.end() - 1);

  for (int n : array) {
    cout<<n<<" ";
  }
  cout<<endl;


  /// III - Two ways of iterating an array
  /// Traditional ways
  for (int i = 0; i < array.size(); ++i) {
    // array[i]
  }

  /// Modern ways
  for (auto beg = array.begin(); beg < array.end(); ++beg) {
    cout<<*beg<<endl;
  }
  return 0;
}

